/*查询数据*/
function InformationCenterquery() {
    $.ajax({
        url:root.brandWebsite.list,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageSize":"30",
            "pageNum":"1",
            "websiteId":$("#websiteId").val(),
            "title":$("#title").val(),
            "startDate":$("#timemin").val(),
            "endDate":$("#timemax").val(),
        }),
        success: function (data) {
            if(data.errorCode==0){
                homePagelist(data.data, 0);
            }else {
                homePagelist("", 0);
            }
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url:root.brandWebsite.list,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageSize":"30",
                            "pageNum":api.getCurrent(),
                            "websiteId":$("#websiteId").val(),
                            "title":$("#title").val(),
                            "startDate":$("#timemin").val(),
                            "endDate":$("#timemax").val(),
                        }),
                        success: function (result) {
                            $("#integralAll")[0].checked = false;
                            homePagelist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
/*表格*/
var dataSource = new Array();
//table的数据源
var table;
dataSource = [];
function homePagelist(data, index){
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
    var datashu =data.startRow;
    $.each(data.newsList, function (i, field) {

        var datashus = datashu++
        var rowData = new Array();
        rowData.push(datashus);
        rowData.push(field.title);
        rowData.push(field.websiteName);
        rowData.push(field.updateTime);
        rowData.push(field.createUser);
        rowData.push(field.newsId);
        dataSource.push(rowData);

    });

    initData(dataSource);
    function initData(dataSource) {
        var dttable = $('#InformationCentertable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#InformationCentertable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": false,
            "bAutoWidth": false,
            "bLengthChange": false,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '<span><td><input class="ids"  type="checkbox" onclick="checkItem()"  value = "' + row[5] + '"></td></span>';
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },


                {
                    "render": function (data, type, row, meta) {
                        var  val = '<a class="ml-10" onclick="carouselSettingsdelete(this,\''+row[5]+'\')">删除</a><a class="ml-10" onclick="homePagedetails(\'详情\',\'InformationDetails.html\',\''+row[5]+'\')">修改</a>';
                        return val;
                    },
                    "targets":6
                },

            ]
        });
    };
}
/*跳转*/
function homePagedetails(title,url,data){
    sessionStorage.setItem("newsId",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//全选
function stopProp() {
    var timer = setTimeout(function() {
        var num = $("#carouselSettingstable").find("tbody").find("input[type=checkbox]:checked").length;
    }, 200)

    window.event ? window.event.cancelBubble = true : e.stopPropagation();
}
function checkItem() {
    var checkBoxValues = $("#carouselSettingstable").find("tbody").find("input[type=checkbox]") ;
    if (checkBoxValues && checkBoxValues.length>0){
        var len = 0 ;
        for (var i=0;i<checkBoxValues.length;i++){
            if(checkBoxValues[i].checked){
                len++ ;
            }
        }
        if (len > 0){
            $('#deleteSelected').removeAttr("href");
            $('#deleteSelected').removeAttr("style");

            var isChecked = len == checkBoxValues.length ;
            $("#integralAll")[0].checked = isChecked;
        }else {
            $("#integralAll")[0].checked = false;
        }
    }

}
//多个删除
function carouselSettingsdeletes() {
    var checkBoxValues = $(".shun").find("input[type=checkbox]:checked") ;
    console.log($(".shun").find("input[type=checkbox]:checked"))


    if (checkBoxValues.length>0){
        var ids = checkBoxValues[0].value;
        for (var i=1;i<checkBoxValues.length;i++){
            if (checkBoxValues[i]){
                ids += ","+checkBoxValues[i].value ;
            }
        }
        var cu = layer.confirm('<span>是否要删除已勾选条目</span>', {title:'提示',btn: ['确认', '取消']}, function(index){
            $.ajax({
                url: root.brandWebsite.listDelete,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data:JSON.stringify({
                    "newsId":ids,
                    "operatorUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
                }),
                success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert('删除成功',2000);
                        InformationCenterquery()
                    }else {
                        $.Huimodalalert(data.msg,2000);
                    }

                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            });
            layer.close(cu);
        },function(){
            layer.close(cu);

        });

    }else {
        $.Huimodalalert('请选择要删除的条目',2000);
    }
}
//单个删除
function carouselSettingsdelete(dom,val) {
    var checkboxs=$(dom).parent().parent().find('.ids')
    var cu = layer.confirm('<span>确定删除吗？</span>', {title:'提示',btn: ['确认', '取消']}, function(index){
        $.ajax({
            url: root.brandWebsite.listDelete,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify({
                "newsId":val,
                "operatorUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
            }),
            success: function (data) {
                if(data.errorCode==0){
                    $.Huimodalalert('删除成功',2000);
                    InformationCenterquery()
                }else {
                    $.Huimodalalert(data.msg,2000);
                }

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
        layer.close(cu);
    },function(){
        layer.close(cu);

    });
}

//设置TDK
function setUp() {
    $("#websiteId2").html("")
    $("#setUp").modal("show");
    $.ajax({
        type: "POST",
        url: root.brandWebsite.query,
        cache: false,  //禁用缓存
        dataType: "json",
        async:false,//优先运行
        contentType: "application/json",
        data: JSON.stringify({

        }),
        success: function (data) {
            for (var i=0;i<data.data.data.length;i++){
                $("#websiteId2").append("<option value=" + data.data.data[i].id + " >" + data.data.data[i].websiteName + "</option>")

            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
    $.ajax({
        type: "GET",
        url: root.brandWebsite.queryNewsTDK,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            "websiteId":$("#websiteId2").val(),
        },
        success: function (data) {
            $("#tdkTitle").val(data.data.tdkTitle)
            $("#tdkDesc").val(data.data.tdkDesc)
            $("#tdkKeyword").val(data.data.tdkKeyword)

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}
//通过官网查询TDK
function TDKquer() {
    $.ajax({
        type: "GET",
        url: root.brandWebsite.queryNewsTDK,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            "websiteId":$("#websiteId2").val(),
        },
        success: function (data) {
            if (data.data!=null) {
                $("#tdkTitle").val(data.data.tdkTitle)
                $("#tdkDesc").val(data.data.tdkDesc)
                $("#tdkKeyword").val(data.data.tdkKeyword)
            }else {
                $("#tdkTitle").val("")
                $("#tdkDesc").val("")
                $("#tdkKeyword").val("")
            }


        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//取消
function closeodel() {
    $("#setUp").modal("hide");
}
//保存
function preservationarreall(){

        if( $("#websiteId2").val()=="" || $("#tdkTitle").val()==""|| $("#tdkDesc").val()==""){
             $.Huimodalalert('请填写TDK',2000);
            return false;
     }
    $.ajax({
        type: "POST",
        url: root.brandWebsite.saveNewsTDK,
        cache: false,  //禁用缓存
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({
            "websiteId":$("#websiteId2").val(),
            "tdkTitle":$("#tdkTitle").val(),
            "tdkDesc":$("#tdkDesc").val(),
            "tdkKeyword":$("#tdkKeyword").val(),
            "operatorUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
        }),
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert('保存成功',2000);
                $("#setUp").modal("hide");
            }else {
                $.Huimodalalert(data.msg,2000);
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

//网站
querybrandWebsites()
function querybrandWebsites() {
    $.ajax({
        type: "POST",
        url: root.brandWebsite.query,
        cache: false,  //禁用缓存
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({

        }),
        success: function (data) {
            for (var i=0;i<data.data.data.length;i++){
                $("#websiteId").append("<option value=" + data.data.data[i].id + " >" + data.data.data[i].websiteName + "</option>")

            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

